package com.yansu.own.controller;

import com.yansu.own.common.dtos.PageResponseResult;
import com.yansu.own.common.dtos.ResponseResult;
import com.yansu.own.common.enums.AppHttpCodeEnum;
import com.yansu.own.entity.dto.SysBannerSearchDto;
import com.yansu.own.entity.dto.SysBannerUpdateDto;
import com.yansu.own.entity.vo.SysBannerVo;
import com.yansu.own.file.service.FileStorageService;
import com.yansu.own.service.SysBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 首页展示标语
 */
@Slf4j
@RestController
@RequestMapping("/sysBanner")
@Api(tags = "首页标语管理")

public class SysBannerController{

    @Autowired
    private SysBannerService bannerService;

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 查询banner表格数据
     */
    @ApiOperation("查询Banner表格数据")
    @GetMapping("/getBannerTable")
    public PageResponseResult getBannerTable(SysBannerSearchDto searchDto) {
        List<SysBannerVo> list = bannerService.getBannerTable(searchDto);
        PageResponseResult pageList = PageResponseResult.getPageList(list);
        return pageList;
    }

    /**
     * 新建Banner
     */
    @ApiOperation("新建Banner")
    @PostMapping("/addBanner")
    public ResponseResult addBanner(@RequestBody @Validated SysBannerUpdateDto insertDto) {
        return bannerService.addBanner(insertDto);
    }

    /**
     * banner图片上传
     */
    @ApiOperation("banner图片上传")
    @PostMapping("/uploadImg")
    public ResponseResult uploadImg(@RequestParam("file") MultipartFile multipartFile) {
        //1，如果图片格式不对，返回参数无效
        if(multipartFile == null || multipartFile.getSize() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2，上传图片到minio，为图片设置UUID文件名
        //2.1 UUID为文件设置文件名
        String fileName = UUID.randomUUID().toString().replace("-","");
        //2.3 获取文件后缀
        String originalFilename = multipartFile.getOriginalFilename();
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String path = null;
        //2.3 上传文件到minio
        try {
            path = fileStorageService.uploadImgFile("/banner/picture", fileName+postfix, multipartFile.getInputStream());
            log.info("banner图片上传成功,");
        } catch (IOException e) {
            e.printStackTrace();
            log.error("banner图片上传失败");
        }
        return ResponseResult.okResult(path);
    }

//    /**
//     * 编辑Banner
//     */
//    @ApiOperation("编辑Banner")
//    @PostMapping("/updateBanner")
//    public ResponseResult updateBanner(@RequestBody BannerManageEntity pojo) {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        pojo.setUpdaterId(loginUser.getUserid());
//        return bannerService.updateBanner(pojo);
//    }
//
//    /**
//     * 查看/预览Banner
//     */
//    @ApiOperation("查看/预览Banner")
//    @GetMapping("/getBanner")
//    public ResponseResult getBanner(int id) {
//        return bannerService.getBanner(id);
//    }
//
//    /**
//     * app端用户获取banner
//     */
//    @ApiOperation("app端用户获取banner")
//    @GetMapping("/getBannerByUser")
//    public ResponseResult getBannerByUser() {
//        return bannerService.getBannerByUser(loginUser.getUserid());
//    }
}
